
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

import 'Utils.dart';

class SPutil{
 SPutil._internal();
 factory SPutil()=>_instance;
 static final SPutil _instance = SPutil._internal();
 static late SharedPreferences _preferences;

 static Future<SPutil> getInstance() async{
   _preferences = await SharedPreferences.getInstance();
   return _instance;
 }

 static setLocalStorage<T>(String key,T value){
   String type = value.runtimeType.toString();

   switch(type){
     case "String":
      return _preferences.setString(key, value as String);
     case "int":
      return _preferences.setInt(key, value as int);
     case "bool":
       return _preferences.setBool(key, value as bool);
     case "double":
       return _preferences.setDouble(key, value as double);
     case "List<String>":
       return _preferences.setStringList(key, value as List<String>);
     case "_InternalLinkedHashMap<String,String>":
       return _preferences.setString(key, json.encode(value));
     case "_InternalLinkedHashMap<String, Object>":
       return _preferences.setString(key, json.encode(value));

   }
 }

 static dynamic getLocalStorage<T>(String key){
  dynamic value = _preferences.get(key);
  if(Utils.isJson(value)){
    return json.decode(value);
  }
  return value;
 }

 static Future<bool> clear() async{
   return await _preferences.clear();
 }


}